package org.atlantis.mina.demo1;

import java.net.InetSocketAddress;

import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;
import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.transport.socket.nio.NioSocketConnector;

/**
 * 一个简单的Mina客户端。
 * 
 * @author <a href="mailto:seenoevil.cn@gmail.com">李曦</a>
 * 
 */
public class SimpleClient {

	public static void main(String[] args) {
		NioSocketConnector connector = new NioSocketConnector();
		// 创建接收数据的过滤器
		DefaultIoFilterChainBuilder chain = connector.getFilterChain();
		// 设定这个过滤器一行一行（/r/n）的读取数据
		chain.addLast("my-chain", new ProtocolCodecFilter(
				new TextLineCodecFactory()));
		// 设定服务器端的消息处理器
		connector.setHandler(new SimpleMinaClientHandler());
		// 设置连接超时
		connector.setConnectTimeoutMillis(30000);
		// 连接到服务器
		ConnectFuture cf = connector.connect(new InetSocketAddress("localhost",
				8888));
		// 等待连接
		cf.awaitUninterruptibly();
		cf.getSession().getCloseFuture().awaitUninterruptibly();
		connector.dispose();
	}
}
